package Day02数据类型_与或;

public class demo_运算符 {
    public static void main(String[] args) {
        int a = 1;
        int b = 3;
        //***基本运算符***
        System.out.println(a + b); //4
        System.out.println(a - b); //-2
        System.out.println(a * b); //3
        System.out.println(a / b); //0
        System.out.println(a % b); //1

        //java整数相除 结果是整数
        //结果要想使小数 必须有小数参与运算

        System.out.println(5 / 3); //1
        System.out.println(5.0 / 3); //1.666666666667
        System.out.println(5 / 3.0); //1.666666666667
        //取余数
        System.out.println(5 % 3); //2
        System.out.println(5 % 5); //0
        System.out.println(5.0 % 3); //2.0

        //   +  在字符串中的应用，起链接作用
        System.out.println("heima" + 2); //heima2
        System.out.println("heima" + 2 + 3); //heima23
        System.out.println("heima" + (2 + 3)); //heima5
        System.out.println(3 + "heima"); //3heima
        System.out.println(3 + 5 + "heima"); //8heima
        System.out.println(3 + (5 + "heima")); //35heima
        //注意运算顺序
        //--------------------------------------------------
        //****赋值运算符****
        //基本赋值 = 将左边赋值给右边  int a = 10;
        //特殊赋值 +=  -=   *=   /=   %=
        int i = 3;
        i += 3; // i = i + 3;输出6
        System.out.println(i);
        i -= 3; // i = i - 3;输出3
        System.out.println(i);
        i *= 3;// i = i * 3;输出9
        System.out.println(i);
        i /= 3;// i = i / 3;输出3
        System.out.println(i);
        i %= 3;// i = i % 3;输出0
        System.out.println(i);

        byte b1 = 2;
        //b = b +5;//错误
        b1 += 5; //输出7,b1为byte类型
        System.out.println(b1);
        //特殊赋值运算符会强制转换为左边类型    b += 5; --> b = (byte)(b + 5);

        //自增++  自减--
        //a++ 后自增1         ++a 先自增1
        // 单独使用
        int c = 10;
        c++;
        System.out.println(c); //11

        int d = 10;
        ++d;
        System.out.println(d); //11
        // 组合使用(了解)
        int m = 10;
        // m++是后++,先做其他操作,先将m赋值给n,n=10,然后m才进行++,m=11
        int n = m++; // 先n=m,然后m++
        System.out.println(m); //11
        System.out.println(n); //10

        // ++x是先++,先做++x,x=11,然后x赋值给y,y=11
        int x = 10;
        int y = ++x; //先x++,然后y=x
        System.out.println(x); //11
        System.out.println(y); //11

        //**面试题**
        int p = 5;
        int q = p++ + ++p + p++; // p++   +       ++p      +            p++
        // 5,后自增变6,先自增变7,7+后自增还是7,运算完后自增变8        ->5+7+7=19
        System.out.println(p);//8
        System.out.println(q);//19

        //**面试题**
        //byte j1 = 1;
        //byte j2 = 2;
        //byte j3 = j1 + j2;//错误 J1+j2是变量运算,是int+int类型,需要转byte.int j3= 1+j2;或者byte j3=(bute)(j1+j2)
        //byte j4 = 1 + 2;  //正确 1+2是常量 不会变

        //Boolean类型只有2个值,不能参与运算

    }
}

